<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="<c:url value="/resources/js/jquery-3.4.1.min.js" />"></script>
<link href="<c:url value="/resources/bootstrap/bootstrap-4.4.1-dist/css/bootstrap.min.css" />" rel="stylesheet">
<script type="text/javascript" src="<c:url value="/resources/bootstrap/bootstrap-4.4.1-dist/js/bootstrap.min.js" />"></script>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"  >
<title>Insert title here</title>
</head>
<body>
<style>
	.wrapper{
		width:30%;
		margin:0 auto;
		margin-top: 100px;
	}
</style>
	<div class="wrapper">
		<form id="login" action="<c:url value="/index.jsp" />" method="get">
		  <div class="form-group">
		    <label for="username">Username</label>
		    <input type="email" class="form-control" id="username" aria-describedby="usernameHelp" name="username" required>
		    <small id="usernameHelp" class="form-text text-muted">We'll never share your info with anyone else.</small>
		  </div>
		  <div class="form-group">
		    <label for="Password">Password</label>
		    <input type="password" class="form-control" id="Password" name="password" required>
		  </div> 
		  <button type="button" class="btn btn-primary" onClick="fnSubmitLoginFrom()">Login</button>
		  <button type="button" class="btn btn-primary" onClick="fnRegistered()">Registered</button>
		</form>
	</div>
</body>
<script type="text/javascript">
	/**
		注册跳转页面js
	*/
	function fnRegistered(){
		location.href = "<c:url value="/registered/jumpRegistered.do" />"
	}
	/**
	       登录AJAX
	*/
	function fnSubmitLoginFrom(){
		var username = $("input[name='username']").val();
		var password = $("input[name='password']").val();
		console.log(username,password)
		if(username == "") {
			alert("请输入用户名！");
			return;
		}
		if(password == "") {
			alert("请输入密码！");
			return;
		}
		var strPostUrl = "<c:url value='/login/submit.do'/>";
		var formData= {
            'username': username,
            'password': password
        };
		$.ajax({
			url: strPostUrl,
			dataType: "json",
			type: "post",
			contentType:"application/json;charset=UTF-8",
			data: JSON.stringify(formData),
		    success: function(res){
		    	console.log(res);
		    	if(res.respCode != "0"){
		    		alert(res.respMsg);
		    	}else{
		    		// 用户验证成功，提交表单
		    		// $("#login").submit();
		    		window.location.href = "<c:url value='/index.jsp' />"
		    	}
		    	/* 
		    	switch (res.respCode)
		    	   case 0:
		    		   alert(res.respMsg);
		    		   break;
                   case -1:
                       alert(res.respMsg);
                       break;
                   case -2:
                       alert(res.respMsg);
                       break;
		    	 */
		    },
	        error: function(){
	        	alert("可能是网络异常等原因，请联系管理员处理")
	        }
		})
	}
</script>
</html>